package com.ss.android.article.browser.database.bookmark;

import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.a.a.s;
import com.ss.android.article.browser.R;
import com.ss.android.article.browser.database.BrowserHistory;
import com.ss.android.common.app.AbsApplication;

import java.util.ArrayList;
import java.util.List;

public class a extends SQLiteOpenHelper implements m {
    private static a a;
    @NonNull private final String b;
    @Nullable private SQLiteDatabase c;

    private a(@NonNull Application arg4) {
        super(((Context)arg4), "bookmarkManager", null, 1);
        this.b = arg4.getString(com.ss.android.article.browser.R.string.a1u);
    }

    public static a a() {
        if(a == null) {
            Class v1 = a.class;
            synchronized (v1){
                if(a == null) {
                    a = new a(AbsApplication.getApplication());
                }
            }
        }

        return a.a;
    }

    static int a(a arg6, String arg7, ContentValues arg8) {
        int v0 = arg6.e().update("bookmark", arg8, "url=?", new String[]{arg7});
        if(v0 == 0) {
            v0 = arg6.e().update("bookmark", arg8, "url=?", new String[]{a.d(arg7)});
        }

        return v0;
    }

    static Cursor a(a arg11, String arg12) {
        String[] v2 = null;
        Cursor v0 = arg11.e().query("bookmark", v2, "url=?", new String[]{arg12}, ((String)null), ((String)null), ((String)null), "1");
        if(v0.getCount() == 0) {
            v0 = arg11.e().query("bookmark", v2, "url=?", new String[]{a.d(arg12)}, ((String)null), ((String)null), ((String)null), "1");
        }

        return v0;
    }

    static SQLiteDatabase a(a arg1) {
        return arg1.e();
    }

    static BrowserHistory a(Cursor arg1) {
        return a.c(arg1);
    }

    @NonNull public final com.a.a.a a(@NonNull BrowserHistory arg2, @NonNull BrowserHistory arg3) {
        return com.a.a.a.a(new com.ss.android.article.browser.database.bookmark.c(this, arg3, arg2));
    }

    @NonNull public final com.a.a.a a(@NonNull String arg2, @NonNull String arg3) {
        return com.a.a.a.a(new k(this, arg3, arg2));
    }

    @NonNull public final s a(@NonNull BrowserHistory arg2) {
        return s.a(new j(this, arg2));
    }

    @NonNull public final s a(@NonNull String arg2) {
        return s.a(new b(this, arg2));
    }

    static int b(a arg6, String arg7) {
        int v0 = arg6.e().delete("bookmark", "url=?", new String[]{arg7});
        if(v0 == 0) {
            v0 = arg6.e().delete("bookmark", "url=?", new String[]{a.d(arg7)});
        }

        return v0;
    }

    static ContentValues b(BrowserHistory arg3) {
        ContentValues v0 = new ContentValues(4);
        v0.put("title", arg3.title);
        v0.put("url", arg3.url);
        v0.put("folder", arg3.c);
        v0.put("position", Integer.valueOf(arg3.g));
        return v0;
    }

    static String b(a arg1) {
        return arg1.b;
    }

    static List b(Cursor arg2) {
        ArrayList v0 = new ArrayList();
        while(arg2.moveToNext()) {
            ((List)v0).add(a.c(arg2));
        }

        arg2.close();
        return ((List)v0);
    }

    @NonNull public final com.a.a.a b(@NonNull String arg2) {
        return com.a.a.a.a(new l(this, arg2));
    }

    @NonNull public final s b() {
        return s.a(new d(this));
    }

    @NonNull private static BrowserHistory c(@NonNull Cursor arg2) {
        BrowserHistory v0 = new BrowserHistory();
        v0.icon = R.drawable.fc;
        v0.setUrl(arg2.getString(arg2.getColumnIndex("url")));
        v0.setTitle(arg2.getString(arg2.getColumnIndex("title")));
        v0.c(arg2.getString(arg2.getColumnIndex("folder")));
        v0.g = arg2.getInt(arg2.getColumnIndex("position"));
        return v0;
    }

    @NonNull public final s c() {
        return s.a(new f(this));
    }

    @NonNull public final s c(@Nullable String arg2) {
        return s.a(new e(this, arg2));
    }

    @NonNull private static String d(@NonNull String arg2) {
        String v0 = arg2.endsWith("/") ? arg2.substring(0, arg2.length() - 1) : arg2 + '/';
        return v0;
    }

    @NonNull public final s d() {
        return s.a(new g(this));
    }

    @NonNull @WorkerThread private SQLiteDatabase e() {
        SQLiteDatabase v0_1;
        synchronized (this){
            if(this.c == null || !this.c.isOpen()) {
                this.c = this.getWritableDatabase();
            }
            v0_1 = this.c;
        }
        return v0_1;
    }

    public void onCreate(@NonNull SQLiteDatabase arg3) {
        arg3.execSQL("CREATE TABLE " + DatabaseUtils.sqlEscapeString("bookmark") + '(' + DatabaseUtils.sqlEscapeString("id") + " INTEGER PRIMARY KEY," + DatabaseUtils.sqlEscapeString("url") + " TEXT," + DatabaseUtils.sqlEscapeString("title") + " TEXT," + DatabaseUtils.sqlEscapeString("folder") + " TEXT," + DatabaseUtils.sqlEscapeString("position") + " INTEGER)");
    }

    public void onUpgrade(@NonNull SQLiteDatabase arg3, int arg4, int arg5) {
        arg3.execSQL("DROP TABLE IF EXISTS " + DatabaseUtils.sqlEscapeString("bookmark"));
        this.onCreate(arg3);
    }
}

